package com.giscloud.amp.controller.sys;

import com.giscloud.amp.entity.SysRegister;
import com.giscloud.amp.entity.dto.SysRegisterDTO;
import com.giscloud.amp.entity.vo.SysRegisterVO;
import com.giscloud.amp.service.sys.SysRegisterService;
import com.giscloud.commons.model.Result;
import com.giscloud.commons.utils.StringUtils;
import com.giscloud.datasource.page.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:gxd
 * @Description:
 * @Date: 11:03 2018/2/26
 * @Modified By:
 */
@SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
@RestController
@Log4j2
public class SysRegisterController {

    @Autowired
    private SysRegisterService sysRegisterRemoteClient;

    @RequestMapping(value="/amp/sysRegister/getList",method = RequestMethod.POST)
    public PageInfo getList(@RequestBody SysRegisterDTO sysRegisterDTO) {
        sysRegisterDTO.setNeedPagination(true);
        return sysRegisterRemoteClient.getSysRegisterList(sysRegisterDTO);
    }

    @RequestMapping(value="/amp/sysRegister/add",method = RequestMethod.POST)
    public Result add(@RequestBody SysRegisterDTO sysRegisterDTO){
        Result result = new Result();
        try {
            if(sysRegisterRemoteClient.insert(sysRegisterDTO) >= 1){
                result.setMsg("提交成功");
                result.setCode(0);
                result.setSuccess(true);
            }else{
                result.setMsg("提交失败");
                result.setCode(-1);
                result.setSuccess(false);
            }
        }catch (Exception e){
            result.setMsg("提交失败");
            result.setCode(-1);
            result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping(value="/amp/sysRegister/edit",method = RequestMethod.POST)
    public Result edit(@RequestBody SysRegisterDTO sysRegisterDTO){
        Result result = new Result();
        if(sysRegisterDTO.getSystemId() == null){
            result.setMsg("系统ID不能为空");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        //修改前查询一次旧值
        SysRegisterVO sysRegisterVO = sysRegisterRemoteClient.getSysRegisterByCondition(sysRegisterDTO);
        sysRegisterDTO.setOldValue(StringUtils.getOldValue(sysRegisterVO));
        SysRegister sysRegister = sysRegisterRemoteClient.update(sysRegisterDTO);
        try {
            if(sysRegister != null){
                result.setMsg("操作成功");
                result.setCode(0);
                result.setSuccess(true);
            }else{
                result.setMsg("操作失败");
                result.setCode(-1);
                result.setSuccess(false);
            }
        }catch (Exception e){
            result.setMsg("操作失败");
            result.setCode(-1);
            result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping(value="/amp/sysRegister/delete",method = RequestMethod.POST)
    public Result delete(@RequestBody SysRegisterDTO sysRegisterDTO){
        Result result = new Result();
        if(sysRegisterDTO.getSystemId() == null){
            result.setMsg("删除失败，系统ID不能为空");
            result.setCode(-1);
            result.setSuccess(false);
            return result;
        }
        try {
            if(sysRegisterRemoteClient.delete(sysRegisterDTO.getSystemId(),sysRegisterDTO.getSystemCode()) >= 1){
                result.setMsg("删除成功");
                result.setCode(0);
                result.setSuccess(true);
            }else{
                result.setMsg("删除失败");
                result.setCode(-1);
                result.setSuccess(false);
            }
        }catch (Exception e){
            result.setMsg("删除失败");
            result.setCode(-1);
            result.setSuccess(false);
        }
        return result;
    }

}
